//
//  NewFeatureController.swift
//  微博
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

class NewFeatureController: UICollectionViewController {

    //初始化方法
    init() {
        //创建一个layout
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(LTCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LTCell
    
        // Configure the cell
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.yellowColor()
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.whiteColor() : UIColor.yellowColor()
        cell.index = indexPath.item
        return cell
    }
    
    //设置cell的代理方法
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取准确的cell
        let cell = collectionView.visibleCells().last as! LTCell
        let index = collectionView.indexPathForCell(cell)
        if index?.item == 3 {
            cell.showAnimation()
        }
    }
}

//cell类
class LTCell:UICollectionViewCell {
    //设置下标属性
    var index:Int = 0 {
        didSet{
            //设置图片,让btn隐藏
            backImageView.image = UIImage(named: "new_feature_\(index + 1)")
            btn.hidden = true
        }
    }
    
    
    //构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加控件
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置动画效果
    private func showAnimation() {
        //先对btn的transform进行修改
        btn.transform = CGAffineTransformMakeScale(0, 0)
        btn.hidden = false
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 0.98, options: [], animations: { () -> Void in
            self.btn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
        }
    }
    
    
    
    
    
    private func setUI() {
        contentView.addSubview(backImageView)
        contentView.addSubview(btn)
        //添加自动布局
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.contentView.snp_edges)
        }
        btn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView.snp_centerX)
            make.top.equalTo(self.contentView.snp_bottom).offset(-150)
        }
        //设置点击事件
        btn.addTarget(self, action: "didStartBtnClick", forControlEvents: .TouchUpInside)
    }
    
    @objc private func didStartBtnClick() {
//        print(__FUNCTION__)
        //点击跳转页面
        NSNotificationCenter.defaultCenter().postNotificationName(rootVC, object: nil)
    }
    
    
    
    //懒加载
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var btn:UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        return btn
    }()
    
    
}








